package com.pitaya.winwine.repository.spec;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

import com.pitaya.winwine.domain.User;
import com.pitaya.winwine.domain.Vintry;
import com.pitaya.winwine.domain.Wine;

public class VintrySpec {
	
	public static Specification<Vintry> findDrinkingPeriodList(final Integer userId) {
		return new Specification<Vintry>() {
			@Override
			public Predicate toPredicate(Root<Vintry> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
				Calendar calendar = Calendar.getInstance();
				calendar.add(Calendar.DATE, 30);
				Join<Vintry, User> joinUser = root.join("user");
				return builder.and(
					builder.equal(joinUser.get("id"), userId), 
					builder.lessThan(root.get("drinkingPeriod").as(Date.class), calendar.getTime())
				);
			}
			
		};
	}
	
	public static Specification<Vintry> findFoodList(final Vintry vintry) {
		return new Specification<Vintry>() {

			@Override
			public Predicate toPredicate(Root<Vintry> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
				List<Predicate> list = new ArrayList<Predicate>();
				Join<Vintry, Wine> wineJoin = root.join("wine");
				list.add(builder.equal(root.get("userId").as(Integer.class), vintry.getUser().getId()));
				if(vintry.getDrinkingType() != null) {
					Calendar calendar = Calendar.getInstance();
					calendar.set(calendar.get(1), 0, 1, 0, 0, 0);
					String sign = "<=";
					if (vintry.getDrinkingType() == 1) {
						calendar.add(Calendar.YEAR, 1);
					}else if (vintry.getDrinkingType() == 2) {
						calendar.add(Calendar.YEAR, 3);
					}else if (vintry.getDrinkingType() == 3) {
						calendar.add(Calendar.YEAR, 8);
					}else {
				        calendar.add(Calendar.YEAR, 8);
				        sign = ">";
					}
					if("<=".equals(sign)) {
						list.add(builder.lessThanOrEqualTo(root.get("drinkingPeriod").as(Date.class), calendar.getTime()));
					}else {
						list.add(builder.greaterThan(root.get("drinkingPeriod").as(Date.class), calendar.getTime()));
					}
				}
				if (vintry.getPriceType() != null) {
					int minPrice, maxPrice;
					if (vintry.getPriceType() == 1) {
						minPrice = 0;
						maxPrice = 200;
					}else if (vintry.getPriceType() == 2) {
						minPrice = 200;
						maxPrice = 500;
					}else if (vintry.getPriceType() == 3) {
						minPrice = 500;
						maxPrice = 1000;
					}else if (vintry.getPriceType() == 4) {
						minPrice = 1000;
						maxPrice = 2000;
					}else {
						minPrice = 2000;
						maxPrice = 99999999;
					}
					list.add(builder.between(root.get("price").as(Integer.class), minPrice, maxPrice));
				}
				if(vintry.getWine().getTypeId() != null) {
					list.add(builder.equal(wineJoin.get("typeId"), vintry.getWine().getTypeId()));
				}
				if(vintry.getWine().getCountryId() != null) {
					list.add(builder.equal(wineJoin.get("countryId"), vintry.getWine().getCountryId()));
				}
				Predicate[] predicates = new Predicate[list.size()];
				for(int i=0; i<list.size(); i++) {
					predicates[i] = list.get(i);
				}
				return query.where(predicates).getGroupRestriction();
			}
			
		};
		
	}
}
